package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.context.BaseContext;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


@Slf4j
@Service
public class DishServicelmpl implements DishService {
    @Autowired
    private DishMapper dishMapper;

    /**
     * 分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageSelect(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        //查询数据
        Page<Dish> page=dishMapper.pageSelect(dishPageQueryDTO);
        //返回数据
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 根据菜品id查询
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> selectById(String categoryId) {
        List<Dish> list=dishMapper.selectById(categoryId);
        return list;
    }

    /**
     * 添加菜品
     * @param dishDTO
     */
    @Override
    public void addDish(DishDTO dishDTO) {
        //定义变量并赋值
        DishFlavor dishFlavor=new DishFlavor();
        Dish dish=Dish.builder()
                .updateTime(LocalDateTime.now())
                        .createTime(LocalDateTime.now())
                                .createUser(BaseContext.getCurrentId())
                                        .updateUser(BaseContext.getCurrentId()).build();
        BeanUtils.copyProperties(dishDTO,dishFlavor);
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.addDish(dish);
        if(dishFlavor.getName()!=null && dishFlavor.getValue()!=null){
            dishMapper.addDishFlavor(dishFlavor);
        }
    }

    /**
     * 通过id查询回显信息
     * @param id
     * @return
     */
    @Override
    public DishVO find(Long id) {
        //创建对象
        DishVO dishVO=new DishVO();
        Dish dish=new Dish();
        //查询并赋值
        dishVO.setFlavors(dishMapper.findFlavors(id));
        dish=dishMapper.findDish(id);
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setId(id);
        return dishVO;
    }

    /**
     * 更新菜品
     * @param dishDTO
     */
    @Override
    public void update(DishDTO dishDTO) {
        //创建对象并赋值
        Dish dish=Dish.builder()
                        .updateUser(BaseContext.getCurrentId())
                                .updateTime(LocalDateTime.now()).build();
        BeanUtils.copyProperties(dishDTO,dish);
        //调用DAO层
        //TODO 只能改一种数据
        dishMapper.updateDish(dish);
        List<DishFlavor> flavor=dishDTO.getFlavors();
        DishFlavor dishFlavor=flavor.get(0);
        dishFlavor.setId(dishDTO.getId());
        dishFlavor.setDishId(dish.getId());
        if(flavor!=null && flavor.size()>0){
          // dishMapper.updateFlavor(dishFlavor);
             dishMapper.deleteFlavor(dishFlavor.getId());
             dishMapper.addDishFlavor(dishFlavor);
        }
    }

    /**
     * 起售或停售商品
     * @param status
     */
    @Override
    public void startOrStop(Integer status,Long id) {
        dishMapper.startOrStop(status,id);

    }

    /**
     * 批量删除菜品
     * @param ids
     */
    @Override
    public void delete(String ids) {
            String[] str= ids.split(",");
            for(int i=0;i<str.length;i++) {
                Long id= Long.valueOf(str[i]);
                dishMapper.deleteDish(id);
            }
        }
        /////
    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.selectById(String.valueOf(dish.getCategoryId()));

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishMapper.findFlavors(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
    }
